<template>
  <div class="supplierManageMent">
    <el-card class="box-card">
      <el-row>
        <el-form>
          <el-col :span="12">
            <el-form-item>
              <el-input
                v-model="queryData.name"
                placeholder="供应商名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-button @click="handlerQuery">查询</el-button>
            <el-button type="primary" @click="handlerAddSupplier"
              >新增供应商</el-button
            >
          </el-col>
        </el-form>
      </el-row>
    </el-card>
    <el-card class="box-card">
      <el-table
        v-loading="loading"
        border
        :data="supplierList"
        style="width: 100%"
      >
        <el-table-column align="center" prop="providerName" label="供应商名称">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handlerEditSupplier(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="deleteSupplier(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <Pagination
        :total="total"
        :page.sync="queryData.pageNum"
        :limit.sync="queryData.pageSize"
        @pagination="getSupplier"
      />
    </el-card>
    <!-- 新增弹窗 -->
    <AddSupplier
      :addDialogVisible="addDialogVisible"
      @SupplierCancel="SupplierCancel"
      @addSupplier="addSupplier"
    />
    <!-- 修改弹窗 -->
    <EditSupplier
      :editDialogVisible="editDialogVisible"
      :editForm="editForm"
      @editSupplierCancel="editSupplierCancel"
      @editAddSupplier="editAddSupplier"
    />
  </div>
</template>
<script>
// 导入接口文件
import {
  getRequest,
  addSupplier,
  deleteSupplier,
  editSupplier,
} from "../../api/supplier";

import Pagination from "../../components/Pagination/index.vue";
import AddSupplier from "./components/addSupplier.vue";
import EditSupplier from "./components/editSupplier.vue";
export default {
  components: {
    AddSupplier,
    EditSupplier,
    Pagination,
  },
  data() {
    return {
      loading: true,
      queryData: {
        pageNum: 1, //当前显示多少数据
        pageSize: 8, //	当前页
        name: "", //角色名称
      },
      total: 0,
      addDialogVisible: false,
      editDialogVisible: false,
      supplierList: [],
      editForm: {},
    };
  },
  created() {
    this.getSupplier();
  },
  methods: {
    // 拆寻按钮
    handlerQuery() {
      this.getSupplier();
    },
    // 获取列表数据
    getSupplier() {
      getRequest(this.queryData).then((res) => {
        if (res.status === 200) {
          this.loading = false;
          this.supplierList = res.data.records;
          this.total = res.data.total; //总条数
          this.queryData.pageNum = res.data.current; //当前页
          this.queryData.pageSize = res.data.size; //每页显示条数
        }
      });
    },
    // 打开新增弹窗按钮
    handlerAddSupplier() {
      this.addDialogVisible = true;
    },
    // 取消新增按钮
    SupplierCancel(valFalse) {
      this.addDialogVisible = valFalse;
    },
    // 确认新增按钮
    addSupplier(valList, valFalse) {
      this.addDialogVisible = valFalse;
      addSupplier(valList).then((res) => {
        if (res.status === 200) {
          this.getSupplier();
          this.$message({
            message: "新增成功",
            type: "success",
          });
        }
      });
    },
    // 打开修改弹窗按钮
    handlerEditSupplier(index, row) {
      this.editDialogVisible = true;
      this.editForm = Object.assign({}, row); //将数据传入dialog页面
      this.editForm.index = index;
    },
    //取消修改按钮
    editSupplierCancel(valFalse) {
      this.editDialogVisible = valFalse;
    },
    // 确认修改按钮
    editAddSupplier(editFrom, valFalse) {
      editSupplier(this.editForm).then((res) => {
        if (res.status === 200) {
          this.getSupplier();
          this.$message({
            message: "修改成功",
            type: "success",
          });
        }
      });

      this.editDialogVisible = valFalse;
    },
    // 删除按钮
    deleteSupplier(rows) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteSupplier(rows.id).then((res) => {
            if (res.status === 200) {
              this.getSupplier();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-col {
  padding-left: 40px;
}
.el-button {
  margin-left: 100px;
}
.el-card {
  margin: 20px;
}
.el-pagination {
  float: right;
  margin: 20px;
}
</style>
